package cn.laixueit.controller;

import cn.laixueit.pojo.Banji;
import cn.laixueit.pojo.Student;
import cn.laixueit.service.BanjiService;
import cn.laixueit.service.StudentService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @Autowired
    private BanjiService banjiService;

    @GetMapping("/studentAdd")
    public String toPageName(Model model){
        List<Banji> banjis = banjiService.all();
        model.addAttribute("banjiList",banjis);
        return "studentAdd";
    }

    @GetMapping("/list")
    public String list(Model model, @RequestParam(defaultValue = "1") Integer startpage,@RequestParam(defaultValue = "5") Integer pagesize,String name,Integer cid){
        PageInfo<Student> pageInfo =  studentService.list(startpage,pagesize,name,cid);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("name",name);
        model.addAttribute("cid",cid);
        List<Banji> banjis = banjiService.all();
        model.addAttribute("banjiList",banjis);
        return "studentList";
    }

    @PostMapping("/save")
    public String  save(Student student){

        studentService.save(student);

        return "redirect:/student/list.do";
    }


    @GetMapping("/del/{id}")
    public String  del(@PathVariable("id")Integer id){

        studentService.del(id);

        return "redirect:/student/list.do";
    }

    @GetMapping("/{id}")
    public String findStudentById(@PathVariable("id")Integer id,Model model){
        Student student = studentService.findStudentById(id);
        model.addAttribute("student",student);
        return "studentEdit";
    }


    @PostMapping("/update")
    public String update(Student student){
        studentService.update(student);
        return "redirect:/student/list.do";
    }


}
